<template>
  <div>
    <editor v-model="myValue" :init="init" :enabled="enabled" :id="tinymceId" />
  </div>
</template>

<script setup>
import { computed, reactive, watch, ref, nextTick, onMounted } from "vue"; //全屏
import { uploadFile } from "@/api/base/im/scene/contentFormatterDocument";

import tinymce from "tinymce/tinymce";
// import "tinymce/skins/content/default/content.css";
import Editor from "@tinymce/tinymce-vue";
import "tinymce/icons/default/icons";
import "tinymce/models/dom"; // 一定要引入
import "tinymce/themes/silver"; // 界面UI主题
import "tinymce/plugins/image";
import "tinymce/plugins/table";
import "tinymce/plugins/lists"; // 列表插件
import "tinymce/plugins/wordcount"; // 文字计数
import "tinymce/plugins/preview"; // 预览
import "tinymce/plugins/emoticons"; // emoji表情
import "tinymce/plugins/emoticons/js/emojis.js"; //必须引入这个文件才有表情图库
import "tinymce/plugins/code"; // 编辑源码
import "tinymce/plugins/link"; // 链接插件
import "tinymce/plugins/advlist"; //高级列表
import "tinymce/plugins/codesample"; //代码示例
import "tinymce/plugins/autoresize"; // 自动调整编辑器大小
import "tinymce/plugins/quickbars"; // 光标处快捷提示
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/searchreplace"; //查找替换
import "tinymce/plugins/autolink"; //自动链接
import "tinymce/plugins/directionality"; //文字方向
import "tinymce/plugins/visualblocks"; //显示元素范围
import "tinymce/plugins/visualchars"; //显示不可见字符
import "tinymce/plugins/charmap"; // 特殊符号
import "tinymce/plugins/nonbreaking"; //插入不间断空格
import "tinymce/plugins/insertdatetime"; //插入日期时间
import "tinymce/plugins/importcss"; //引入自定义样式的css文件
import "tinymce/plugins/accordion"; // 可折叠数据手风琴模式
import "tinymce/plugins/anchor"; //锚点
import "tinymce/plugins/fullscreen";


const emits = defineEmits(["update:modelValue", "setHtml"]);
//这里我选择将数据定义在props里面，方便在不同的页面也可以配置出不同的编辑器，当然也可以直接在组件中直接定义
const props = defineProps({
  modelValue: {
    type: String,
    default: () => {
      return "";
    },
  },
  baseUrl: {
    type: String,
    default: "",
  },
  enabled: {
    type: Boolean,
    default: true,
  },
  // 编辑器初始可编辑状态
  editable_root: {
    type: Boolean,
    default: true,
  },
  plugins: {
    type: [String, Array],
    default: "importcss autoresize searchreplace autolink directionality code visualblocks visualchars fullscreen image link codesample table charmap nonbreaking anchor insertdatetime advlist lists wordcount charmap quickbars emoticons accordion",
  },
  knwlgId: {
    type: String,
  },
  toolbar: {
    type: [String, Array, Boolean],
    default: "undo redo | accordion accordionremove | blocks fontfamily fontsize| bold italic underline strikethrough ltr rtl  | align numlist bullist | link image | table | lineheight outdent indent| forecolor backcolor removeformat | charmap emoticons | anchor codesample",
  },
  readonly: {
    type: Boolean,
    default: false,
  },
  minHeight: {
    type: Number,
    default: 630,
  },
  contentMargins: {
    type: Object,
    default: () => ({ top: 2.54, bottom: 2.54, left: 3.17, right: 3.17 }) // 默认Word标准边距(厘米)
  }
});
const loading = ref(false);
const tinymceId = ref(
  "vue-tinymce-" + +new Date() + ((Math.random() * 1000).toFixed(0) + "")
);

const font_family_formats = ref(` 
  宋体=SimSun;
  仿宋=FangSong;
  仿宋_GB2312=FangSong_GB2312;
  楷体=KaiTi;
  楷体_GB2312=KaiTi_GB2312;
  黑体=SimHei;
  微软雅黑='Microsoft YaHei';
  微软正黑体='Microsoft JhengHei';
  华文宋体=STSong;
  华文仿宋=STFangsong;
  华文楷体=STKaiti;
  华文黑体=STHeiti;
  华文中宋=STZhongsong; 
  方正小标宋=FZXiaoBiaoSong-B05;
  方正仿宋=FZFangSong-Z02;
  方正楷体=FZKai-Z03;
  方正黑体=FZHei-B01;
  思源宋体=Noto Serif SC;
  思源黑体=Noto Sans SC;
   
  Arial=Arial, Helvetica, sans-serif;
  Helvetica=Helvetica, Arial, sans-serif;
  Verdana=Verdana, Geneva, sans-serif;
  Tahoma=Tahoma, Geneva, sans-serif;
  Segoe UI='Segoe UI', Tahoma, Geneva, sans-serif;
  Calibri=Calibri, Candara, Segoe, sans-serif;
  Trebuchet MS='Trebuchet MS', Helvetica, sans-serif;
  Gill Sans='Gill Sans', 'Gill Sans MT', Calibri, sans-serif;
   
  Times New Roman='Times New Roman', Times, serif;
  Georgia=Georgia, Times, serif;
  Palatino=Palatino, 'Palatino Linotype', serif;
  Garamond=Garamond, Baskerville, 'Baskerville Old Face', serif;
  Book Antiqua='Book Antiqua', Palatino, serif;
   
  Courier New='Courier New', Courier, monospace;
  Consolas=Consolas, monaco, monospace;
  Monaco=Monaco, Consolas, monospace;
   
  Impact=Impact, Charcoal, sans-serif;
  Comic Sans MS='Comic Sans MS', cursive;
  Lucida Sans Unicode='Lucida Sans Unicode', 'Lucida Grande', sans-serif;
   
  微软雅黑 + Arial='Microsoft YaHei', Arial, sans-serif;
  宋体 + Times New Roman=SimSun, 'Times New Roman', serif;
  黑体 + Helvetica=SimHei, Helvetica, sans-serif;
  思源黑体 + Segoe UI='Noto Sans SC', 'Segoe UI', sans-serif
`) ; 


// 更新边距的方法，直接接收厘米值
const updateMargins = (margins) => {
  const editor = tinymce.get(tinymceId.value);
  if (editor) {
    editor.dom.setStyles(editor.getBody(), {
      marginTop: `${margins.top}cm`,
      marginBottom: `${margins.bottom}cm`,
      marginLeft: `${margins.left}cm`,
      marginRight: `${margins.right}cm`,
      padding: '0'
    });
  }
};

// 厘米转像素的转换函数
const cmToPx = (cm) => {
  return cm * 37.8; // 1cm ≈ 37.8px
};

//定义一个对象 init初始化
const init = ({
  selector: "#" + tinymceId.value, //富文本编辑器的id,
  language_url: "/tinymce/langs/zh_CN.js", // 语言包的路径，具体路径看自己的项目
  language: "zh_CN",
  // skin_url: "/tinymce/skins/ui/tinymce-5", // skin路径，具体路径看自己的项目
  skin_url: "/tinymce/skins/ui/oxide", // skin路径，具体路径看自己的项目
  editable_root: props.editable_root,
  height: 900,
  branding: false, // 是否禁用“Powered by TinyMCE”
  promotion: false, //去掉 upgrade

  toolbar: props.toolbar, //这里的数据是在props里面就定义好了的
  toolbar_location: "top" , 
  // toolbar_sticky: true,
  // toolbar_sticky_offset: 10,
  toolbar_mode: "wrap", // 工具栏模式 floating / sliding / scrolling / wrap  
  menubar: "edit view insert format tools table",
  paste_data_images: true, //允许粘贴图像
  image_dimensions: false, //去除宽高属性
  plugins: props.plugins, //这里的数据是在props里面就定义好了的
  // 取消图片资源路径转换
  convert_urls: false,
  // table边框位0是否展示网格线
  visual: false,
  // 超链接默认打开方式
  link_default_target: "_blank",
  link_context_toolbar: true,
  // 默认快捷菜单
  quickbars_insert_toolbar: "image codesample table",
  // 选中图片的快捷提示
  quickbars_image_toolbar: "alignleft aligncenter alignright | rotateleft rotateright | imageoptions",
  editimage_toolbar: "rotateleft rotateright | flipv fliph | editimage imageoptions",
  // 文字样式
  font_family_formats: font_family_formats.value ,   
  font_size_formats: "初号=42pt 小初=36pt 一号=26pt 小一=18pt 二号=22pt 小二=18pt 三号=16pt 小三=11.25pt 四号=10.5pt 小四=9pt 五号=7.875pt 小五=6.75pt 六号=5.625pt 七号=4.875pt 八号=3.75pt 6pt 7.5pt 8.25pt 36pt 48pt 54pt",
  image_caption: true,
  editimage_cors_hosts: ["picsum.photos"],
  noneditable_class: "mceNonEditable",
  // 默认样式
  content_style: "body { font-family:Helvetica,Arial,sans-serif; font-size:16px }p {margin:3px; line-height:24px;}",
  image_advtab: true,
  importcss_append: true,
  paste_webkit_styles: "all",
  paste_merge_formats: true,
  nonbreaking_force_tab: false,
  paste_auto_cleanup_on_paste: false,
  file_picker_types: "file",
  // 选中文字的快捷提示
  quickbars_selection_toolbar: "bold italic | quicklink h2 h3 blockquote quickimage quicktable",
  // 编辑器高度自适应
  autoresize_bottom_margin: 20,
  // autoresize_overflow_padding: 16,
  min_height: props.minHeight,
  content_css: "/tinymce/skins/content/default/content.css", //以css文件方式自定义可编辑区域的css样式，css文件需自己创建并引入
  setup: function (editor) {
    editor.on('init', function() {

      if (editor.getContent() === '') {
        editor.setContent('<p style="color: #888;">请输入内容...</p>');
      }

      // 初始化时应用边距
      editor.dom.setStyles(editor.getBody(), {
        marginTop: `${props.contentMargins.top}cm`,
        marginBottom: `${props.contentMargins.bottom}cm`,
        marginLeft: `${props.contentMargins.left}cm`,
        marginRight: `${props.contentMargins.right}cm`,
        padding: '0'
      });
    });

    editor.on('focus', function () {
      if (editor.getContent() === '<p style="color: #888;">请输入内容...</p>') {
        editor.setContent('');
      }
    });
    editor.on('blur', function () {
      if (editor.getContent() === '') {
        editor.setContent('<p style="color: #888;">请输入内容...</p>');
      }
    });
  },
  paste_preprocess: function(plugin, args) {
    // 处理粘贴的内容中的图片
    args.content = args.content.replace(/<img[^>]+src="file:[^>]+>/g, function(img) {
      return '<img src="" alt="图片需手动上传">';
    });
  },
  // images_upload_handler: async function (blobInfo, progress) {
  //   return new Promise(async (resolve, reject) => {
  //     try {
  //       const blob = blobInfo.blob();
  //       // 非base64图片需要上传
  //       if (blob.size / 1024 / 1024 > 200) {
  //         reject({
  //           message: "上传失败，文件大小请控制在 200M 以内",
  //           remove: true,
  //         });
  //         return;
  //       }
        
  //       const res = await uploadFile(blob, progress);
  //       // 根据实际API返回结构调整
  //       resolve(res.data.url || res.url);
        
  //     } catch (error) {
  //       if (error.message) {
  //         reject(error.message);
  //       } else {
  //         reject("上传失败");
  //       }
  //     }
  //   });
  // },
  // //图片上传  -实列 具体请根据官网补充-
  // images_upload_handler: async function (blobInfo, progress) {
  //   try {
  //     const file = blobInfo.blob();
  //     if (file.size / 1024 / 1024 > 10) {
  //       throw {
  //         message: "上传失败，图片大小请控制在 20M 以内",
  //         remove: true,
  //       };
  //     }
      
  //     const res = await uploadFile(file, progress);
  //     resolve(res.data.base64);
  //   } catch (error) {
  //     if (error.message) {
  //       // TinyMCE显示错误消息
  //       return Promise.reject(error.message);
  //     }
  //     return Promise.reject("上传失败");
  //   }
  // },
 
});

// 外部传递进来的数据变化
const myValue = computed({
  get() {
    return props.modelValue;
  },
  set(val) {
    emits("update:modelValue", val);
  },
});

//监听富文本中的数据变化
watch(
  () => myValue.value,
  () => {
    emits(
      "setHtml",
      tinymce.activeEditor.getContent({ format: "text" }),
      myValue.value
    );
  }
);

// 设置编辑器只读模式
watch(
  () => props.readonly,
  (newValue, oldValue) => {
    nextTick(() => {
      tinymce.activeEditor.mode.set(newValue ? "readonly" : "design");
      let iframeDom = document.querySelector("iframe");
      iframeDom &&
        (iframeDom.contentWindow.document.body.style.margin = newValue
          ? 0
          : "16px");
    });
  },
  { immediate: true }
);

//初始化编辑器
onMounted(() => {
  tinymce.init({});
});

// 设置值
const handleSetContent = (content) => {
  tinymce.activeEditor.setContent(content);
};

// 获取值
const handleGetContent = () => {
  return tinymce.activeEditor.getContent();
};

defineExpose({
  handleSetContent,
  handleGetContent,
  updateMargins,
});
</script>

<style lang="scss" scoped>
:deep(.tox-tinymce) {
  border: 1px solid #dcdfe6;
  border-radius: 4px;

  .tox-statusbar {
    display: none;
  }
}
</style>